package sk.tramtatiri.pages.users;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.tramtatiri.core.FoundFood;
import sk.tramtatiri.core.Ingredient;
import sk.tramtatiri.core.Parser;
import sk.tramtatiri.data.dao.interfaces.FoodDao;
import sk.tramtatiri.data.dao.interfaces.RecipeDao;
import sk.tramtatiri.data.dataobjects.Food;
import sk.tramtatiri.data.dataobjects.Recipe;

public class RecipeCreatePanel extends Panel {

	private static final long serialVersionUID = 9068403964361464535L;

	@SpringBean
	private FoodDao foodDao;
	@SpringBean
	private RecipeDao recipeDao;

	private String result;
	private TextField<String> recipeName;
	private TextArea<String> recipeIngredients;
	private TextArea<String> recipeProcedure;

	public RecipeCreatePanel(String id) {
		super(id);
		
		final ModalWindow modal = new ModalWindow("recipeCreateModal");
        add(modal);
        
        recipeName = new TextField<String>("recipeName", Model.of("[Zadajte meno receptu]"));
        recipeName.setRequired(true);
		recipeName.setLabel(new Model<String>("recipeName"));
		
		recipeIngredients = new TextArea<String>("recipeIngredients", Model.of("[Zadajte ingrediencie v tvare [ [meno] [hmotnost], ] ] jablko 2kg, orechy 30g, rasca 3kg, mlieko 2l, voda 1l, zemiaky 500g"));
		recipeIngredients.setRequired(true);
		recipeIngredients.setLabel(new Model<String>("recipeIngredients"));
		
		recipeProcedure = new TextArea<String>("recipeProcedure", Model.of("[Zadajte postup pripravy]"));
		recipeProcedure.setRequired(true);
		recipeProcedure.setLabel(new Model<String>("recipeProcedure"));
		
		Form<?> form = new Form<Void>("formRecipeCreate");

		form.add(new AjaxButton("showModal") {

			private static final long serialVersionUID = 4144535803282461571L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> arg1) {
				Recipe recipe = recipeDao.findName(recipeName.getInput());
				if(recipe == null){
					Parser parser = new Parser(recipeIngredients.getInput());
					Object[] object = getIngredients(parser.getIngredients());
					
					modal.setContent(new RecipeCreateModal(modal.getContentId(), modal,(List<FoundFood>) object[0], (List<Ingredient>)object[1], recipeName.getInput(), recipeProcedure.getInput()));
			        modal.setTitle("Vytvorit recept: "+ recipeName.getInput());
			        modal.setCookieName("modal-2");
			        modal.setWindowClosedCallback(new ModalWindow.WindowClosedCallback(){
			            public void onClose(AjaxRequestTarget target){
			            	RequestCycle rc = RequestCycle.get();
							rc.setResponsePage(RecipesPage.class);
			            }
			        });
			        modal.show(target);
				} else {
					//TODO cerveny text nazvu receptu
				}
			}
		});
				
		add(form);
		
		form.add(recipeName);
		form.add(recipeIngredients);
		form.add(recipeProcedure);
    }

	/**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * @param result
	 *            the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}

	private Object[] getIngredients(List<Ingredient> ingredients) {
		List<FoundFood> foundFoods = new ArrayList<FoundFood>();
		List<Ingredient> notFoundingredients = new ArrayList<Ingredient>();
		if (!ingredients.isEmpty()) {
			for (Ingredient ingredient : ingredients) {
				Food food = foodDao.findName(ingredient.getName());
				if (food != null) {
					foundFoods.add(new FoundFood(food, ingredient.getWeight(), ingredient.getValue()));
				}else {
					notFoundingredients.add(ingredient);
				}
			}
		}

		return new Object[] { foundFoods, notFoundingredients };
	}
}
